package com.detech.sbom.business.login.serviceimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.detech.sbom.base.config.mybatis.PageParam;
import com.detech.sbom.base.mybatis.login.dao.TbSysLogininforDao;
import com.detech.sbom.base.mybatis.login.entity.TbSysLogininforEntity;
import com.detech.sbom.business.login.service.ISysLogininforService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class SysLogininforServiceImpl implements ISysLogininforService {

    @Resource
    public TbSysLogininforDao tbSysLogininforDao;

    @Override
    public Page<TbSysLogininforEntity> selectPageLogininforList(PageParam pageParam, String ipaddr, String status, String userName, String msg, String accessTime, String deviceType) {
        LambdaQueryWrapper<TbSysLogininforEntity> lqw = new LambdaQueryWrapper<TbSysLogininforEntity>()
            .like(StrUtil.isNotBlank(ipaddr), TbSysLogininforEntity::getIpaddr, ipaddr)
            .like(StrUtil.isNotBlank(msg), TbSysLogininforEntity::getMsg, msg)
            .eq(StrUtil.isNotBlank(status), TbSysLogininforEntity::getStatus, status)
            .eq(StrUtil.isNotBlank(deviceType), TbSysLogininforEntity::getDeviceType, deviceType)
            .like(StrUtil.isNotBlank(userName), TbSysLogininforEntity::getUsername, userName);
        if(StrUtil.isNotBlank(accessTime)){
            Date logTimeDate = DateUtil.parse(accessTime,"yyyy-MM-dd").toJdkDate();
            Date logtimeAddOne = DateUtil.offsetDay(logTimeDate,1).toJdkDate();
            lqw.between(TbSysLogininforEntity::getAccessTime ,logTimeDate,logtimeAddOne);
        }
        if (StrUtil.isBlank(pageParam.getOrderByColumn())) {
            pageParam.setOrderByColumn("access_time");
            pageParam.setIsAsc("desc");
        }
        Page<TbSysLogininforEntity> page = tbSysLogininforDao.selectPage(pageParam.build(), lqw);
        return  page;
    }

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    public int insertLogininfor(TbSysLogininforEntity logininfor) {
        return tbSysLogininforDao.insert(logininfor);
    }

    /**
     * 查询操作日志详细
     *
     * @param infoId 操作ID
     * @return 操作日志对象
     */
    @Override
    public TbSysLogininforEntity selectLogininforById(String infoId) {
        return tbSysLogininforDao.selectById(infoId);
    }

}
